package com.scau.databaseexam.controllers;


import com.scau.databaseexam.DAO.UserDao;
import com.scau.databaseexam.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    UserDao userDao;


    @PostMapping(value = "/user/login")
    public String login(User user,
                        Map<String,String> map,
                        HttpSession session){

        System.out.println(user);
        User temp = (User) userDao.selectUser(user.getId());
        System.out.println(temp);
        //使用user可以避免数据库查询为null的错误
        if(user.equals(temp)){
            session.setAttribute("loginUser",temp);
            //防止表单重复提交，使用重定向
            return "redirect:/dashboard.html";
        } else if(temp==null){
            map.put("msg","user not exist!");
        } else {
            map.put("msg", "id or name Wrong!");
        }
            return "login";
    }


}
